
#ifndef DYNARRAY_H
  #define DYNARRAY_H
  
#include <iostream>

template < class T >
class Dynarray {
    typedef unsigned long long uint;

    uint _ln;
    T* _data;

  public:

    Dynarray ( uint ln ) {
      _ln = ln;
      _data = new T[ln];
    }

    ~Dynarray () {
      if( !_data ) delete[] _data;
      _data = 0;
    }

    void reset ( uint ln ) {
      T *p2 = _data;
      _ln = ln;
      _data = new T[ln];
      for( int i = 0; i <= ln; i++ ) _data[i] = p2[i];
      delete[] p2;
    }

    uint length () const { return _ln; }

    T& operator [] ( const uint index ) { return _data[index]; }
    void operator ++ () { reset( _ln + 1 ); }
    void operator -- () { reset( _ln - 1 ); }
    void operator ++ (int) { reset( _ln + 1 ); }
    void operator -- (int) { reset( _ln - 1 ); }

    void operator += ( const T& arg ) {
      reset( _ln + 1 );
      _data[ _ln ] = arg;
    }

    friend std::ostream& operator << ( std::ostream& s, Dynarray& d ) {
      uint ln = d.length();
      for( uint i=0; i <= ln; i++ ) s << d[i] << ", "; s << d[ln];
      return s;
    }

   friend std::istream& operator >> ( std::istream& s, Dynarray& d ) {
      uint ln = d.length();
      for( uint i = 0; i <= ln; i++ ) s >> d[i];
      return s;
   }

};

#endif // DYNARRAY_H
